<template>
	<div class="all">
		<div class="left">
			<left></left>
		</div>
		<div class="right">
			<div class="top">
				<top></top>
			</div>
			<div class="content">
				<div class="flex">
					<el-button type="warning" @click="log = true">添加分销商</el-button>
				</div>
				<div class="table">
					<el-table :data="tableData" border style="width: 100%">
						<el-table-column label="代理商名称">
							<template slot-scope="scope">
								<div>{{ scope.row.realname }}</div>
							</template>
						</el-table-column>
						<el-table-column label="手机号">
							<template slot-scope="scope">
								<span style="margin-left: 10px">{{ scope.row.phone }}</span>
							</template>
						</el-table-column>
						<el-table-column label="状态">
							<template slot-scope="scope">
								<span style="margin-left: 10px" v-if="scope.row.status == 1">正常</span>
								<span style="margin-left: 10px" v-if="scope.row.status == 2">冻结</span>
							</template>
						</el-table-column>
						<el-table-column label="创建时间">
							<template slot-scope="scope">
								<span style="margin-left: 10px">{{ scope.row.create_time }}</span>
							</template>
						</el-table-column>
						<el-table-column label="操作">
							<template slot-scope="scope">
								<el-button type="text" @click="shanchu(scope.row.id)">删除</el-button>
							</template>
						</el-table-column>
					</el-table>
					<el-pagination style="text-align: right; padding: 20px;" background layout="total,prev, pager, next" :current-page="page"
					 @current-change="fanye" :page-size="page_size" :total="total"></el-pagination>
				</div>
			</div>
		</div>

		<el-dialog title="添加分销商" :visible.sync="log" :close-on-click-modal="false" width="600px">
			<div class="flex">
				<div class="f1">分销商名称:</div>
				<div class="f2">
					<el-select v-model="fenxiaoshangid" placeholder="请选择分销商">
						<el-option v-for="item in fenxiaoshang" :key="item.id" :label="item.agentName" :value="item.id"></el-option>
					</el-select>
				</div>
			</div>

			<span slot="footer" class="dialog-footer">
				<el-button @click="log = false">取 消</el-button>
				<el-button type="primary" @click="tianjia">确 定</el-button>
			</span>
		</el-dialog>


	</div>
</template>

<script>
	import top from '@/components/top.vue'
	import left from '@/components/left.vue'
	export default {
		name: 'index',
		components: {
			left,
			top
		},
		data() {
			return {
				fenxiaoshang: "",
				fenxiaoshangid: "",
				log: false,
				page: 1,
				page_size: 20,
				total: 0,
				loading: false,
				tableData: []
			}
		},
		created() {
			this.getlist()
			this.getlist2()
		},
		methods: {
			shanchu: function(id) {
				this.$confirm('是否真的删除此分销商?', '确认提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.axios.post(this.url+`/work/packages/deleAgentWithPackageId?agent_id=${id}&package_id=`+this.$route.query.id)
						.then(response => {
							if (response.data.success == true) {

								this.loading = true
								this.gettaocan()
								this.$message.error("已删除套餐");
							} else {
								this.$message.error(response.data.message);
							}
						})
						.catch(response => {
							if (response.response.status == 401) {
								this.$router.push({
									path: '/login'
								})
							}
						});
				})
			},
			go: function(id) {
				this.$router.push({
					path: "/index_taocan",
					query: {
						id: id
					}
				})
			},
			//翻页
			fanye: function(page) {
				this.loading = true
				this.page = page
				this.getlist()
			},
			//获取列表
			getlist: function() {
				this.axios.get(this.url+`/work/packages/findAgentWithPacageId?id=`+this.$route.query.id)
					.then(response => {
						if (response.data.success == true) {
							this.tableData = response.data.result
						} else {
							this.$message.error(response.data.message);
						}
					})
					.catch(response => {
						if (response.response.status == 401) {
							this.$router.push({
								path: '/login'
							})
						}
					});
			},
			//获取全部分销商
			getlist2: function() {
				this.axios.get(this.url+`/work/agent/list?pageNo=${this.page}&pageSize=1000`)
					.then(response => {
						if (response.data.success == true) {
							this.fenxiaoshang = response.data.result.records
						} else {
							this.$message.error(response.data.message);
						}
					})
					.catch(response => {
						if (response.response.status == 401) {
							this.$router.push({
								path: '/login'
							})
						}
					});
			},
			//分销商绑定套餐
			tianjia: function() {
				this.axios.post(this.url+`/work/agent/addPackageWithAgentId?agent_id=${this.fenxiaoshangid}&package_id=`+this.$route.query.id)
					.then(response => {
						if (response.data.success == true) {
							this.$message.success("添加分销商成功");
							this.fenxiaoshangid = ""
							this.getlist()
							this.log = false
						} else {
							this.$message.error(response.data.message);
						}
					})
					.catch(response => {
						if (response.response.status == 401) {
							this.$router.push({
								path: '/login'
							})
						}
					});
			}
		},

	}
</script>

<style scoped lang="less">
	.all {
		display: flex;
		height: 100vh;

		.left {
			width: 200px;
			height: 100%;
			background: #414652;
			box-shadow: 0 2px 10px #2D3038;
		}

		.right {
			width: calc(100% - 180px);
			height: 100%;
			overflow: auto;

			.content {
				padding: 20px;

				.table {
					margin-top: 20px;
				}
			}
		}
	}
</style>
